package com.ruoyi.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.param.impl.ChangeRecordParam;
import com.param.impl.GetUserDataByIdRequestParam;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.manager.domain.ChangeRecord;
import com.ruoyi.pay.manager.service.IChangeRecordService;
import com.ruoyi.user.domain.MgUser;
import com.ruoyi.user.service.IMgUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 余额变更记录Controller
 * 
 * @author 王强
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/recharge/record")
public class ChangeRecordController extends BaseController
{
    @Autowired
    private IChangeRecordService changeRecordService;
    
    private IMgUserService mgUserService;

    /**
     * 查询余额变更记录列表
     */
    @PreAuthorize("@ss.hasPermi('recharge:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChangeRecord changeRecord)
    {
        startPage();
        List<ChangeRecord> list = changeRecordService.selectList(changeRecord);
        return getDataTable(list);
    }
    
    /**
	 * 根据用户id查询余额变更列表
	 */
	@PreAuthorize("@ss.hasPermi('recharge:record:list')")
	@GetMapping("/listByUser")
	public TableDataInfo listByUser(@Validated GetUserDataByIdRequestParam dataByIdRequestParam) {
		startPage();
		
		return getDataTable(this.changeRecordService.selectList(dataByIdRequestParam));
	}
    

    /**
     * 导出余额变更记录列表
     */
    @PreAuthorize("@ss.hasPermi('recharge:record:export')")
    @Log(title = "余额变更记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChangeRecord changeRecord)
    {
        List<ChangeRecord> list = changeRecordService.selectList(changeRecord);
        ExcelUtil<ChangeRecord> util = new ExcelUtil<ChangeRecord>(ChangeRecord.class);
        util.exportExcel(response, list, "余额变更记录数据");
    }

    /**
     * 获取余额变更记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('recharge:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(changeRecordService.selectById(id));
    }

    /**
     * 新增余额变更记录
     */
    @PreAuthorize("@ss.hasPermi('finance:change:money')")
    @Log(title = "余额变更记录", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody @Validated ChangeRecordParam changeRecord)
    {
    	
    	MgUser selectMgUserById = mgUserService.selectMgUserById(changeRecord.getUserId() + "");
    	
    	int changeType = changeRecord.getChangeType().intValue();
    	
    	BigDecimal academicCurrency = selectMgUserById.getAcademicCurrency();

    	BigDecimal money = changeRecord.getMoney();
    	
    	if ( 0 == changeType ) {
    		// 出账
    		BigDecimal zero = new BigDecimal(0);
        	
			BigDecimal subtract = academicCurrency.subtract(money);
    		if (-1 == subtract.compareTo(zero)) {
        		// 余额不够 
        		return AjaxResult.error("余额不够");
        	}
    		selectMgUserById.setAcademicCurrency(subtract);
    	} else {
    		// 进账
    		selectMgUserById.setAcademicCurrency(academicCurrency.add(money));
    	}
    	
    	mgUserService.updateRecharge(selectMgUserById);
    	
        return toAjax(changeRecordService.insertParam(changeRecord));
    }

    /**
     * 修改余额变更记录
     */
    @PreAuthorize("@ss.hasPermi('recharge:record:edit')")
    @Log(title = "余额变更记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChangeRecord changeRecord)
    {
        return toAjax(changeRecordService.update(changeRecord));
    }

    /**
     * 删除余额变更记录
     */
    @PreAuthorize("@ss.hasPermi('recharge:record:remove')")
    @Log(title = "余额变更记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(changeRecordService.deleteByIds(ids));
    }

    @Resource
	public void setMgUserService(IMgUserService mgUserService) {
		this.mgUserService = mgUserService;
	}
}
